package com.example.manpower.Server.impl;

import cn.hutool.core.img.gif.NeuQuant;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.manpower.Dto.DepartmentEmployeeCount;
import com.example.manpower.Entity.Salary;
import com.example.manpower.Entity.User;
import com.example.manpower.Mapper.SalaryMapper;
import com.example.manpower.Server.PositionService;
import com.example.manpower.Server.UserService;
import com.example.manpower.Mapper.UserMapper;
import com.example.manpower.Util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
* @author asus
* @description 针对表【user】的数据库操作Service实现
* @createDate 2024-10-11 09:56:26
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SalaryMapper salaryMapper;

    @Autowired
    private PositionServiceImpl positionService;
    @Override
    public Result selectById(User user) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",user.getUserId());
        queryWrapper.eq("status",1);
        User user1 = userMapper.selectOne(queryWrapper);
        return new Result(200,"查询成功",user1);
    }

    @Override
    public Result selectPage(User user) {
        // 创建分页对象
        Page<User> page = new Page<>(user.getPage(), user.getSize());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);

        // 如果用户输入了用户名，则添加模糊查询条件
        if (user.getUserName() != null) {
            queryWrapper.like("user_name", user.getUserName());
        }

        // 分页查询用户
        userMapper.selectPage(page, queryWrapper);
        List<User> list = page.getRecords();

        // 遍历用户列表，查询薪资总和
        for (User currentUser : list) {
            // 从salary表中获取该用户的薪资总和
            Double userSalary = salaryMapper.getTotalSalaryByUserId(currentUser.getUserId());
            // 将薪资总和设置到当前用户对象中
            currentUser.setUserSalary(BigDecimal.valueOf(userSalary));
        }

        // 返回结果
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("total", page.getTotal());
        return new Result(200, "查询成功", map);
    }



    @Override
    public Result delete(User user) {
        user.setStatus(0);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",user.getUserId());
        int num = userMapper.update(user, queryWrapper);
        salaryMapper.delete(queryWrapper);
        return new Result(200,"删除用户成功",num);
    }

    @Override
    public Result add(User user) {
        String positionname = positionService.selectpositionName(user.getPostitionId());
        user.setPositionName(positionname);

        int i = positionService.selectdepartmentId(user.getPostitionId());
        user.setDepartmentId(i);

        if (user.getUserCarid() != null && user.getUserCarid() != "") {
            int num = userMapper.insert(user);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_carid",user.getUserCarid());
            User user1 = userMapper.selectOne(queryWrapper);
            if (user1 != null) {
                Salary salary = new Salary();
                salary.setUserId(user1.getUserId());
                salaryMapper.insert(salary);
            }
            return new Result(200,"添加用户成功",num);
        }
        return new Result(400,"未填写身份证号插入失败",null);
    }

    @Override
    public Result update(User user) {
        user.setPostitionId(user.getPostitionId());

        String positionname = positionService.selectpositionName(user.getPostitionId());
        user.setPositionName(positionname);

        int departmentId = positionService.selectdepartmentId(user.getPostitionId());
        user.setDepartmentId(departmentId);

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",user.getUserId());
        User user1 = userMapper.selectOne(queryWrapper);
        if(user1!=null){
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper.eq("user_id",user.getUserId());
            int num = userMapper.update(user, queryWrapper);
            return new Result(200,"修改用户成功",num);
        }
        return new Result(400,"修改用户失败",null);
    }

    @Override
    public Result countTotalEmployees() {
        // 创建 QueryWrapper，用于构建查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1); // 状态为 1 的员工
        Long l = userMapper.selectCount(queryWrapper);
        // 使用 MyBatis-Plus 的 selectCount 方法获取状态为 1 的员工总数
        return new Result(200,"查询成功",l);
    }

    @Override
    public Result countTotalDepartments() {
        Long d = userMapper.selectDistinctDepartmentCount();
        // 查询不同的部门ID，计算部门数量
        return new Result(200,"查询成功",d);
    }


}




